﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Media;
using road2.code;

namespace road2.Model
{
    public abstract class TrafficManagerClass : BaseRoadObj
    {
        private RoadClass _road;

        public RoadClass Road
        {
            get { return _road; }
            private set { _road = value; RaisePropertyChanged("Road"); }
        }

        public TrafficManagerClass(Guid id, RoadClass road) : base(id)
        {
            this.Road = road;
        }
    }

    public class TrafficGeneratorClass : TrafficManagerClass
    {
        private double _vpi = 1.0;

        public double VPI
        {
            get { return _vpi; }
            set { _vpi = value; RaisePropertyChanged("VPI"); }
        }

        public TrafficGeneratorClass(Guid id, RoadClass road)
            : base(id, road)
        {

        }

        public CarClass GenCar(IEnumerable<CarClass> carCollection)
        {
            if (road2.Services.GlobalRandomClass.Random.NextDouble() > VPI)
            {
                return null;
            }

            var cars = carCollection.Where(c=>c.Road == this.Road).Where(c=>c.Distance < 50);

            if (cars.Count() != 0)
            {
                return null;
            }

            CarClass car = new CarClass(Guid.NewGuid());
            car.Color = Colors.Orange;
            car.MaxSpeed = 90;

            CarRoadManager.PlaceCarToRoad(car, this.Road);

            return car;
        }
    }

    public class TrafficTerminatorClass : TrafficManagerClass
    {
        public TrafficTerminatorClass(Guid id, RoadClass road)
            : base(id, road)
        {

        }

        public void TermCar(CarClass car)
        {
            car.Road = null;
            //car = null;
        }
    }
}
